package view.Core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class A3Activity extends Activity {

	public static final int login = Menu.FIRST + 0;
	public static final int DataSynchronization = Menu.FIRST + 1;
	private static final int REQUEST_CODE = 3434;// /login out

	TextView loginTitle;
	boolean loginStatic = false;
	String loginName, Password;

	private Object[] activities = { "Find Tree",
			encodeActivity.class, "Insert Tree", insertTreeActivity.class,
			"Registion Tree", registionTreeActivity.class, "Update Tree",
			selectUpdateTreeActivity.class

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maindisplay);

		loginTitle = (TextView)findViewById(R.id.loginTitle);
		
		CharSequence[] list = new CharSequence[activities.length / 2];
		for (int i = 0; i < list.length; i++) {
			list[i] = (String) activities[i * 2];
		}

		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_list_item_1, list);
		ListView listView = (ListView) findViewById(R.id.ListView01);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(A3Activity.this,
						(Class<?>) activities[position * 2 + 1]);
				/*
				 * Bundle bundle = new Bundle(); bundle.putString("loginName",
				 * loginName); intent.putExtras(bundle);
				 */
				startActivity(intent);
			}
		});
	}

	
	public boolean onCreateOptionsMenu(Menu menu) {

		String txt;
		if (loginStatic == true) {
			txt = "logout";
		} else {
			txt = "login";
		}

		menu.add(0, login, 0, "login/out");
		return true;
	}

	
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case login: {
			if (loginStatic == false) {
				Intent intent = new Intent();
				intent.setClass(this, loginActivity.class);
				startActivityForResult(intent, REQUEST_CODE);
			} else {
				Toast.makeText(this, "Logout", Toast.LENGTH_LONG).show();
				loginStatic = false;
				loginTitle.setText("You have not been login.");
			}

			break;
		}

		}
		return true;
	}
	// ///check login static
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("loginName")) {
				Toast.makeText(this, "You now can access the system!",
						Toast.LENGTH_LONG).show();
				loginName = data.getExtras().getString("loginName");
				Password = data.getExtras().getString("Password");
				loginStatic = true;
				loginTitle.setText("Welcome " + loginName);

			}
		}
	}

	public void errorMSG() {
		Toast.makeText(this, "To do this," + '\n' + "You should login First!!",
				Toast.LENGTH_LONG).show();
	}

	/*
	 * if (resultCode == REQUEST_Data_S_CODE && requestCode ==
	 * REQUEST_Data_S_CODE) { if (data.hasExtra("loginName")) {
	 * Toast.makeText(this, "You now can access the system!",
	 * Toast.LENGTH_LONG).show(); } }
	 */

}